/*
 * kthread.cpp
 *
 *  Created on: Dec 7, 2011
 *      Author: kylintse
 */

#include "kthread.h"

kthread::kthread(QObject* parent /*= 0*/)
	: QThread(parent)
{
	m_exitFlag=false;
	m_startFlag=false;
}

kthread::~kthread()
{

}

bool kthread::needExit()
{
	QMutexLocker lock(&m_lockExit);
	return m_exitFlag;
}

void kthread::setStarted(bool flag)
{
	m_lockStart.lock();
	m_startFlag=flag;
	m_lockStart.unlock();
	m_waitStart.wakeAll();
}

bool kthread::start(int timeout)
{
	QThread::start();
	m_lockStart.lock();
	if(m_startFlag)
	{
		m_lockStart.unlock();
		return true;
	}
	if(!m_waitStart.wait(&m_lockStart,timeout))
	{
		m_lockStart.unlock();
		return false;
	}
	else
	{
		m_lockStart.unlock();
		return m_startFlag;
	}
}

void kthread::exit()
{
	QMutexLocker lock(&m_lockExit);
	m_exitFlag=true;
}

